﻿using BloggerAutoPosterService.Engine.Utils;
using System.Windows.Forms;
using System.IO;

namespace BloggerAutoPosterService.Engine.Core
{
    public class ServiceConfig : IServiceConfig
    {
        #region Fields (1)

        readonly bool _useApplicationPath;

        #endregion Fields

        #region Constructors (1)

        public ServiceConfig(bool useApplicationPath)
        {
            _useApplicationPath = useApplicationPath;
        }

        #endregion Constructors

        #region Properties (18)

        public string BlogUrl
        {
            get { return ConfigManager.GetConfigData("BlogUrl"); }
        }

        public string DBName
        {
            get
            {
                if (!_useApplicationPath)
                    return ConfigManager.GetConfigData("DBName");

                var path = Path.Combine(Application.StartupPath, Path.GetFileName(ConfigManager.GetConfigData("DBName")));
                if (!File.Exists(path))
                {
                    File.WriteAllText(path, @"<?xml version=""1.0"" encoding=""utf-8""?><DailyEntries></DailyEntries>");
                }
                return path;
            }
        }

        public string ErrorsLogFile
        {
            get
            {
                if (!_useApplicationPath)
                    return ConfigManager.GetConfigData("ErrorsLogFile");

                return Path.Combine(Application.StartupPath, Path.GetFileName(ConfigManager.GetConfigData("ErrorsLogFile")));
            }
        }

        public string[] FeedToParse
        {
            get { return ConfigManager.GetConfigData("FeedToParse").Split('\n'); }
        }

        public string[] GooglePlusUserId
        {
            get { return ConfigManager.GetConfigData("GooglePlusUserId").Split('\n'); }
        }

        public bool IsProxyEnabled
        {
            get { return ConfigManager.GetConfigData("IsProxyEnabled").ToLowerInvariant() == "true"; }
        }

        public string Password
        {
            get { return ConfigManager.GetConfigData("Password"); }
        }

        public string PostAt
        {
            get { return ConfigManager.GetConfigData("PostAt"); }
        }

        public string ProjectRSSFeed
        {
            get { return ConfigManager.GetConfigData("ProjectRSSFeed", throwException: false); }
        }

        public string ProxyServerAddress
        {
            get { return ConfigManager.GetConfigData("ProxyServerAddress"); }
        }

        public string ProxyServerPassword
        {
            get { return ConfigManager.GetConfigData("ProxyServerPassword"); }
        }

        public int ProxyServerPort
        {
            get { return int.Parse(ConfigManager.GetConfigData("ProxyServerPort")); }
        }

        public string ProxyServerUserName
        {
            get { return ConfigManager.GetConfigData("ProxyServerUserName"); }
        }

        public int ReadSitesDataIntervalMin
        {
            get { return int.Parse(ConfigManager.GetConfigData("ReadSitesDataIntervalMin")); }
        }

        public string Tag
        {
            get { return ConfigManager.GetConfigData("Tag"); }
        }

        public string Title
        {
            get { return ConfigManager.GetConfigData("Title"); }
        }

        public bool UsePersianDate
        {
            get { return ConfigManager.GetConfigData("UsePersianDate").ToLowerInvariant() == "true"; }
        }

        public string UserName
        {
            get { return ConfigManager.GetConfigData("UserName"); }
        }

        #endregion Properties
    }
}
